library(tidyverse)
library(rdrobust)
library(marginaleffects)
library(haven)
library(here)

# read EP post election survey
d_ees = read.csv2(here("04_Gewessler/data/ZA8868_v1-0-0.csv"))
d2 = d_ees %>% filter(country_alpha2 == "AT")
d_ees = d2 %>%
  mutate(date = as.Date(meta_end_date),
         survey = "ees",
         responded = T,
         treat = date >= as.Date("2024-06-17"),
         running = as.numeric(date - as.Date("2024-06-17")) + 0.5,
         gov = na_if(q4, 98),
         party_EP = case_when(
           q6_recoded == 4001 ~ "FPO",
           q6_recoded == 4002 ~ "OVP",
           q6_recoded == 4003 ~ "SPO",
           q6_recoded == 4004 ~ "GRUN",
           q6_recoded == 4005 ~ "NEOS",
           q6_recoded == 4006 ~ "KPO",
           q6_recoded == 4007 ~ "DNA",
           q6_recoded == 4008 ~ "BIER",
           q6_recoded == 4009 ~ "JETZT",
           q6_recoded == 4010 ~ "WANDEL",
           q6_recoded == 4011 ~ "MFG",
         ),
         party_nat = case_when(
           q8_recoded == 4001 ~ "FPO",
           q8_recoded == 4002 ~ "OVP",
           q8_recoded == 4003 ~ "SPO",
           q8_recoded == 4004 ~ "GRUN",
           q8_recoded == 4005 ~ "NEOS",
           q8_recoded == 4006 ~ "KPO",
           q8_recoded == 4007 ~ "DNA",
           q8_recoded == 4008 ~ "BIER",
           q8_recoded == 4009 ~ "JETZT",
           q8_recoded == 4010 ~ "WANDEL",
           q8_recoded == 4011 ~ "MFG",
         ),
         ptv_FPO = na_if(q9_5, 98),
         ptv_OVP = na_if(q9_1, 98),
         ptv_SPO = na_if(q9_2, 98),
         ptv_GRUN = na_if(q9_4, 98),
         ptv_NEOS = na_if(q9_3, 98),
         ptv_KPO = na_if(q9_6, 98),
         ptv_BIER = na_if(q9_7, 98),

         lr_FPO = na_if(q11_5, 98),
         lr_OVP = na_if(q11_1, 98),
         lr_SPO = na_if(q11_2, 98),
         lr_GRUN = na_if(q11_4, 98),
         lr_NEOS = na_if(q11_3, 98),
         lr_KPO = na_if(q11_6, 98),
         lr_BIER = na_if(q11_7, 98),

         news = na_if(q7, 98),
         climate = na_if(q12_5, 98),
         climate5 = case_when(climate %in% 0:2 ~ 1,
                              climate %in% 3:4 ~ 2,
                              climate %in% 5 ~ 3,
                              climate %in% 6:7 ~ 4,
                              climate %in% 8:10 ~ 5),
         EUmembership = case_when(q16 == 1 ~ 2,
                                  q16 == 2 ~ 0,
                                  q16 == 3 ~ 1),
         EUintegration = na_if(q17, 98),
         EUdemocracy = na_if(q3, 98),
         strongleader = na_if(q13_2, 98),
         interest = na_if(q15, 98),
         lr = na_if(q10, 98),
         edu = na_if(Edu_rec,98),
         edu2 = na_if(d2, 98),
         edu2 = na_if(edu2, 97),
         age = d4_age,
         gender = case_when(
           d3 == 1 ~ "male",
           d3 == 2 ~ "female"
         ),
         rural = d8,
         trade_union_raw = na_if(d1, 98),
         trade_union = case_when(trade_union_raw %in% c(1, 3) ~ 1,
                                 trade_union_raw %in% c(2, 4) ~ 0),
         employment_raw = na_if(d6, 96),
         student = case_when(employment_raw == 3 ~ 1,
                             !is.na(employment_raw) ~ 0),
         unemployed = case_when(employment_raw == 5 ~ 1,
                                 !is.na(employment_raw) ~ 0),
         retired = case_when(employment_raw == 3 ~ 1,
                             !is.na(employment_raw) ~ 0),
         democracy = na_if(q2, 98),
         lrdist_GRUN = abs(lr - lr_GRUN),
         lrdist_OVP = abs(lr - lr_OVP),
         lr_tri = case_when(lr < 5 ~ "left",
                            lr > 5 ~ "right",
                            T ~ "center")
         )
hist(as.Date(d_ees$meta_start_date), breaks = "days", freq = T)
table(d_ees$climate)

# read autnes
load(here("04_Gewessler/data/10874_da_de_v1_0.RData"))
tablab = sapply(table,
                function(x){attr(x,"label")}) %>% unlist()
tablab = data.frame(varname = names(tablab),
                    labs = tablab)

d_autnes = table %>%
  mutate(date = w20_date,
         survey = "autnes",
         responded = !is.na(w20_intdur),
         party_EP = case_when(w20_q9 == 1 ~ "OVP",
                              w20_q9 == 2 ~ "SPO",
                              w20_q9 == 3 ~ "FPO",
                              w20_q9 == 4 ~ "GRUN",
                              w20_q9 == 5 ~ "NEOS",
                              w20_q9 == 6 ~ "KPO",
                              w20_q9 == 7 ~ "DNA"),
         ptv_SPO = ifelse(w20_q35x1<11,w20_q35x1, NA),
         ptv_OVP = ifelse(w20_q35x2<11, w20_q35x2, NA),
         ptv_FPO = ifelse(w20_q35x3<11, w20_q35x3, NA),
         ptv_GRUN = ifelse(w20_q35x4<11, w20_q35x4, NA),
         ptv_NEOS = ifelse(w20_q35x5<11, w20_q35x5, NA),
         ptv_KPO = ifelse(w20_q35x6<11, w20_q35x6, NA),
         ptv_BIER = ifelse(w20_q35x7<11,w20_q35x7 , NA),

         ptv_SPO_pre = coalesce(w19_q13x1, w18_q7x1, w17_q11x1),
         ptv_SPO_pre = ifelse(ptv_SPO_pre < 11, ptv_SPO_pre, NA),
         ptv_OVP_pre = coalesce(w19_q13x2, w18_q7x2, w17_q11x2),
         ptv_OVP_pre = ifelse(ptv_OVP_pre < 11, ptv_OVP_pre, NA),
         ptv_FPO_pre = coalesce(w19_q13x3, w18_q7x3, w17_q11x3),
         ptv_FPO_pre = ifelse(ptv_FPO_pre < 11, ptv_FPO_pre, NA),
         ptv_GRUN_pre = coalesce(w19_q13x4, w18_q7x4, w17_q11x4),
         ptv_GRUN_pre = ifelse(ptv_GRUN_pre < 11, ptv_GRUN_pre, NA),
         ptv_NEOS_pre = coalesce(w19_q13x5, w18_q7x5, w17_q11x5),
         ptv_NEOS_pre = ifelse(ptv_NEOS_pre < 11, ptv_NEOS_pre, NA),
         ptv_KPO_pre = coalesce(w19_q13x6, w18_q7x6, w17_q11x6),
         ptv_KPO_pre = ifelse(ptv_KPO_pre < 11, ptv_KPO_pre, NA),
         ptv_BIER_pre = coalesce(w19_q13x7, w18_q7x7, w17_q11x7),
         ptv_BIER_pre = ifelse(ptv_BIER_pre < 11, ptv_BIER_pre, NA),

         gov_original = ifelse(w20_q36 < 5, w20_q36, NA),
         gov = ifelse(gov_original %in% 1:2, 1, # CAUTION this is satisfaction, other survey is approval
                      ifelse(gov_original %in% 3:4, 2, NA)),
         lr = coalesce(w19_q46, w20f_q61, w18f_q15, w17f_q38, w16_q14, w15f_q25) %>%
           ifelse(. < 11, ., NA),
         lr_pre = coalesce(w19_q46, w18f_q15, w17f_q38, w16_q14, w15f_q25) %>%
           ifelse(. < 11, ., NA),
         lr_SPO = coalesce(w19_q45x1, w20f_q60x1, w18f_q14x1, w17f_q37x1, w16_q13x1, w15f_q24x1) %>%
           ifelse(. < 11, ., NA),
         lr_OVP = coalesce(w19_q45x2, w20f_q60x2, w18f_q14x2, w17f_q37x2, w16_q13x2, w15f_q24x2) %>%
           ifelse(. < 11, ., NA),
         lr_OVP_pre = coalesce(w19_q45x2, w18f_q14x2, w17f_q37x2, w16_q13x2, w15f_q24x2) %>%
           ifelse(. < 11, ., NA),
         lr_FPO = coalesce(w19_q45x3, w20f_q60x3, w18f_q14x3, w17f_q37x3, w16_q13x3, w15f_q24x3) %>%
           ifelse(. < 11, ., NA),
         lr_GRUN = coalesce(w19_q45x4, w20f_q60x4, w18f_q14x4, w17f_q37x4, w16_q13x4, w15f_q24x4) %>%
           ifelse(. < 11, ., NA),
         lr_GRUN_pre = coalesce(w19_q45x4, w18f_q14x4, w17f_q37x4, w16_q13x4, w15f_q24x4) %>%
           ifelse(. < 11, ., NA),
         lr_NEOS = coalesce(w19_q45x5, w20f_q60x5) %>%
           ifelse(. < 11, ., NA),
         lr_KPO = coalesce(w19_q45x6, w20f_q60x6) %>%
           ifelse(. < 11, ., NA),
         lr_BIER = coalesce(w19_q45x7, w20f_q60x7) %>%
           ifelse(. < 11, ., NA),
         lr_tri = case_when(lr < 5 ~ "left",
                            lr > 5 ~ "right",
                            T ~ "center"),
         gender = coalesce(as.numeric(sd3_y24), as.numeric(sd3_y22), as.numeric(sd3_y19), as.numeric(sd3)),
         gender = case_when(
             gender == 1 ~ "male",
             gender == 2 ~ "female",
             T ~ NA
           ),
         age = coalesce(as.numeric(age_y24), as.numeric(age_y22 + 2),
                        as.numeric(age_y19 + 3), as.numeric(age + 5)),
         rural_raw = coalesce(as.numeric(sd24_y24), as.numeric(sd24_y22),
                              as.numeric(sd24_y19), as.numeric(sd24)),
         rural = case_when(rural_raw == 1 ~ 1,
                           rural_raw %in% 2:3 ~ 2,
                           rural_raw %in% 4:5 ~ 3),
         trade_union = coalesce(as.numeric(sd21_y24), as.numeric(sd21_y22),
                                as.numeric(sd21_y19), as.numeric(sd21)),
         trade_union = case_when(trade_union == 2 ~ 0,
                                 trade_union == 1 ~ 1),
         employment_raw = coalesce(as.numeric(sd10_y24), as.numeric(sd10_y22),
                                   as.numeric(sd10_y19), as.numeric(sd10)),
         student = case_when(employment_raw %in% 3:5 ~ 1,
                             !is.na(employment_raw) ~ 0),
         unemployed = case_when(employment_raw == 10 ~ 1,
                                !is.na(employment_raw) ~ 0),
         retired = case_when(employment_raw == 2 ~ 1,
                             !is.na(employment_raw) ~ 0),
         edu_raw = coalesce(sd7_y24, sd7_y22,
                            sd7_y19, sd7),
         edu2 = case_when(edu_raw == 3 ~ 1,
                         edu_raw == 4 ~ 2,
                         edu_raw %in% 6:9 ~ 3,
                         edu_raw %in% 10 ~ 4,
                         edu_raw %in% 11:13 ~ 5,
                         edu_raw %in% 14:16 ~ 6),
         edu = case_when(edu2 %in% 1:2 ~ 1,
                         edu2 %in% 3:4 ~ 2,
                         edu2 %in% 5:6 ~ 3),
        interest = as.numeric(w20_q1),
        EUmembership = case_when(w20_q20x1 %in% 1:2 ~ 2,
                                 w20_q20x1 == 3 ~ 1,
                                 w20_q20x1 %in% 4:5 ~ 0),
        EUintegration = na_if(as.numeric(w20_q16), 88),

        trust_police = as.numeric(na_if(w20_q4x3, 88)),
        trust_judiciary = as.numeric(na_if(w20_q4x6, 88)),
        fear_climatechange = w20_q54x1,
        fear_russia = w20_q54x2,
        fear_immigration = w20_q54x3,
        fear_econ = w20_q54x5,
        fear_crime = w20_q54x5,
        fear_rightextremism = w20_q54x6,
        situation_confident = w20_q53x1,
        situation_worried = w20_q53x2,
        situation_upset = w20_q53x3,
        situation_anxious = w20_q53x4,
        situation_hopeful = w20_q53x5,
        situation_angry = w20_q53x6,

        mip_environment = ifelse(is.na(w20_q28x18), 0, 1),
        mip_climate = ifelse(is.na(w20_q28x8), 0, 1),
        mip_immigration = ifelse(is.na(w20_q28x4), 0, 1),
        mip_climateenvironment = ifelse(mip_environment == 1 | mip_climate == 1, 1, 0),

        lrdist_GRUN = abs(lr - lr_GRUN),
        lrdist_OVP = abs(lr - lr_OVP),
        climate5 = as.numeric(w16_q26x8),
        climate5 = na_if(climate5, 88),
        lr_tri = case_when(lr < 5 ~ "left",
                           lr > 5 ~ "right",
                           T ~ "center"),
        lr_tri_pre = case_when(lr_pre < 5 ~ "left",
                               lr_pre > 5 ~ "right",
                               T ~ "center"),
        turnout_prospective = w20_q30 %>%
          ifelse(. < 11, ., NA),
        panelentry = as.numeric(panelentry)
        )

# combine
d_combined = bind_rows(d_ees, d_autnes)
d_combined = d_combined %>% filter(!is.na(date))
d_combined = d_combined %>%
  mutate(treat = date >= as.Date("2024-06-17"),
         running = as.numeric(date - as.Date("2024-06-17")) + 0.5)

# keep only subset variables
d_combined = d_combined %>% select(
  date, survey, responded, treat, running,
  party_EP, party_nat,
  gov,
  ptv_FPO, ptv_OVP, ptv_SPO, ptv_GRUN, ptv_NEOS, ptv_KPO, ptv_BIER,
  ptv_FPO_pre, ptv_OVP_pre, ptv_SPO_pre, ptv_GRUN_pre, ptv_NEOS_pre, ptv_KPO_pre, ptv_BIER_pre,
  lr, lr_pre,
  lr_FPO, lr_OVP, lr_SPO, lr_GRUN, lr_NEOS, lr_KPO, lr_BIER,
  lr_OVP_pre, lr_GRUN_pre,
  lr_tri, lr_tri_pre,
  lrdist_GRUN, lrdist_OVP,
  EUmembership, EUintegration, EUdemocracy, strongleader, democracy,
  interest,
  climate, climate5, news,
  age, gender, edu, edu2, rural,
  trade_union_raw, trade_union,
  employment_raw, student, unemployed, retired,
  trust_police, trust_judiciary,
  fear_climatechange, fear_russia, fear_immigration, fear_econ, fear_crime, fear_rightextremism,
  situation_confident, situation_worried, situation_upset, situation_anxious, situation_hopeful, situation_angry,
  mip_environment, mip_climate, mip_immigration, mip_climateenvironment,
  turnout_prospective, panelentry
)

# compute additional variables
d_combined = d_combined %>%
  mutate(ptv_own = case_when(
    party_EP == "OVP" ~ ptv_OVP,
    party_EP == "SPO" ~ ptv_SPO,
    party_EP == "FPO" ~ ptv_FPO,
    party_EP == "GRUN" ~ ptv_GRUN,
    party_EP == "NEOS" ~ ptv_NEOS,
    party_EP == "KPO" ~ ptv_KPO
  ))
d_combined$gov_satis = ifelse(d_combined$gov == 2, 0, d_combined$gov)

# impute averages of lr party placements for voters who didn't complete this
d_combined$lr_OVP[is.na(d_combined$lr_OVP)] = mean(d_combined$lr_OVP, na.rm = T)
d_combined$lr_GRUN[is.na(d_combined$lr_GRUN)] = mean(d_combined$lr_GRUN, na.rm = T)

# pre-treatment versions
d_combined$lr_OVP_pre[is.na(d_combined$lr_OVP_pre)] = mean(d_combined$lr_OVP_pre, na.rm = T)
d_combined$lr_GRUN_pre[is.na(d_combined$lr_GRUN_pre)] = mean(d_combined$lr_GRUN_pre, na.rm = T)

# compute left of compromise
d_combined$leftofcompromise = (d_combined$lr - 0.5 * (d_combined$lr_OVP + d_combined$lr_GRUN)) < 0
hist(0.5 * (d_combined$lr_OVP + d_combined$lr_GRUN))

d_combined$leftofcompromise_pre = (d_combined$lr_pre - 0.5 * (d_combined$lr_OVP_pre + d_combined$lr_GRUN_pre)) < 0

# as tri-variable
d_combined$leftofcompromise_tri = case_when((d_combined$lr - 0.5 * (d_combined$lr_OVP + d_combined$lr_GRUN)) < 0 ~ "left",
                                            (d_combined$lr - 0.5 * (d_combined$lr_OVP + d_combined$lr_GRUN)) > 0 ~ "right",
                                            (d_combined$lr - 0.5 * (d_combined$lr_OVP + d_combined$lr_GRUN)) == 0 ~ "center")

# compute additional variables
d_combined$leftofcompromise_label = ifelse(d_combined$leftofcompromise, "Left of compromise", "Right of compromise")
d_combined$gender_num = as.numeric(d_combined$gender == "female")
d_combined$interest_flipped = d_combined$interest * (-1) + 4
d_combined$polinterest = case_when(d_combined$interest < 3 ~ "high",
                                   d_combined$interest > 2 ~ "low")
d_combined$age_scaled = scale(d_combined$age)[,1]
d_combined$gender_num_scaled = scale(d_combined$gender_num)[,1]
d_combined$edu2_scaled = scale(d_combined$edu2)[,1]
d_combined$rural_scaled = scale(d_combined$rural)[,1]
d_combined$trade_union_scaled = scale(d_combined$trade_union)[,1]
d_combined$student_scaled = scale(d_combined$student)[,1]
d_combined$unemployed_scaled = scale(d_combined$unemployed)[,1]
d_combined$retired_scaled = scale(d_combined$retired)[,1]

# save
saveRDS(d_combined, here("data_combined.RDS"))

# Read all waves (minimal for placebo test)
d_autnes_all =
  bind_rows(
    table %>%
      mutate(date = w1_date,
             wave = 1,
             ptv_GRUN = ifelse(w1_q12x4<11, w1_q12x4, NA),
             lr = coalesce(w1_q9) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w2_date,
             wave = 2,
             ptv_GRUN = ifelse(w2_q5x4<11, w2_q5x4, NA),
             lr = coalesce(w1_q9) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w3_date,
             wave = 3,
             ptv_GRUN = ifelse(w3_q8x3<11, w3_q8x3, NA),
             lr = coalesce(w1_q9) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w4_date,
             wave = 4,
             ptv_GRUN = ifelse(w4_q10x4<11, w4_q10x4, NA),
             lr = coalesce(w4f_q8, w1_q9) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w7_date,
             wave = 7,
             ptv_GRUN = ifelse(w7_q18x4<11, w7_q18x4, NA),
             lr = coalesce(w7_q17) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w8_date,
             wave = 8,
             ptv_GRUN = ifelse(w8_q61x4<11, w8_q61x4, NA),
             lr = coalesce(w8_q15) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w9_date,
             wave = 9,
             ptv_GRUN = ifelse(w9_q52x4<11, w9_q52x4, NA),
             lr = coalesce(w9f_q54, w8_q15) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w10_date,
             wave = 10,
             ptv_GRUN = ifelse(w10_q50x4<11, w10_q50x4, NA),
             lr = coalesce(w10f_q81, w8_q15) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w11_date,
             wave = 11,
             ptv_GRUN = ifelse(w11_q10x4<11, w11_q10x4, NA),
             lr = coalesce(w11f_q7, w8_q15) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w12_date,
             wave = 12,
             ptv_GRUN = ifelse(w12_q53x4<11, w12_q53x4, NA),
             lr = coalesce(w12_q9) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w13_date,
             wave = 13,
             ptv_GRUN = ifelse(w13_q39x4<11, w13_q39x4, NA),
             lr = coalesce(w13_q5) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w14_date,
             wave = 14,
             ptv_GRUN = ifelse(w14_q13x4<11, w14_q13x4, NA),
             lr = coalesce(w14_q12) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w15_date,
             wave = 15,
             ptv_GRUN = ifelse(w15_q33x4<11, w15_q33x4, NA),
             lr = coalesce(w15f_q25, w14_q12) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w16_date,
             wave = 16,
             ptv_GRUN = ifelse(w16_q15x4<11, w16_q15x4, NA),
             lr = coalesce(w16_q14) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w17_date,
             wave = 17,
             ptv_GRUN = ifelse(w17_q11x4<11, w17_q11x4, NA),
             lr = coalesce(w17f_q38, w16_q14) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w18_date,
             wave = 18,
             ptv_GRUN = ifelse(w18_q7x4<11, w18_q7x4, NA),
             lr = coalesce(w18f_q15, w16_q14) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w19_date,
             wave = 19,
             ptv_GRUN = ifelse(w19_q13x4<11, w19_q13x4, NA),
             lr = coalesce(w19_q46) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w20_date,
             wave = 20,
             ptv_GRUN = ifelse(w20_q35x4<11, w20_q35x4, NA),
             lr = coalesce(w20f_q61, w19_q46) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w21_date,
             wave = 21,
             ptv_GRUN = ifelse(w21_q11x4<11, w21_q11x4, NA),
             lr = coalesce(w21f_q24, w19_q46) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w22_date,
             wave = 22,
             ptv_GRUN = ifelse(w22_q9x4<11, w22_q9x4, NA),
             lr = coalesce(w22_q48) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit(),
    table %>%
      mutate(date = w23_date,
             wave = 23,
             ptv_GRUN = ifelse(w23_q9x4<11, w23_q9x4, NA),
             lr = coalesce(w23f_q37, w22_q48) %>%
               ifelse(. < 11, ., NA),
             lr_tri = case_when(lr < 5 ~ "left",
                                lr > 5 ~ "right",
                                T ~ "center")) %>%
      select(date, wave, ptv_GRUN, lr, lr_tri) %>% na.omit()
  )

# compute
d_autnes_all = d_autnes_all %>% group_by(wave) %>%
  mutate(treat_date = median(date, na.rm = F),
         treat = date >= treat_date,
         running = as.numeric(date - treat_date) + 0.5)

# save
saveRDS(d_autnes_all, here("data_autnes_all.RDS"))

